﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookManage.BLL;
using BookManage.Model;
using System.Data.SqlClient;

namespace BookManage
{
    public partial class frmReaderType : Form
    {
        private DataTable dt = null;//存放查询结果，并给DataGridView dgvReader提供数据
        private ReaderType rt = new ReaderType();//存放读者类型信息，与读者类型信息控件组内的各控件进行数据交换，并与BLL、Model层进行数据交换
        private ReaderTypeAdmin readerTypeBLL = new ReaderTypeAdmin();

        public frmReaderType()
        {
            InitializeComponent();
            
        }
        private void ShowData()  //输出dgvReaderType中表格数据
        {
            dgvReaderType.DataSource = dt;
            foreach (DataColumn dc in dt.Columns)
            {
               dgvReaderType.Columns[dc.ColumnName].HeaderText = ReaderType.ColumnTitle(dc.ColumnName);
            }
        }
        #region 读者信息组内控件与实体类对象之间的数据互换
        private void SetReaderTypeToText() //将实体类对象转换成控件
        {
            txtrdType.Text = Convert.ToString(rt.rdType);
            txtrdTypeName.Text = rt.rdTypeName;
            txtCanLendDay.Text = Convert.ToString(rt.CanLendQty);
            txtCanLendQty.Text = Convert.ToString(rt.CanLendDay);
            txtCanContinueTimes.Text = Convert.ToString(rt.CanContinueTimes);
            txtPunishRate.Text = Convert.ToString(rt.PunishRate);
            txtDateValid.Text = Convert.ToString(rt.DateValid);
        }

        private void SetTextToReaderType() //将控件上的数据转换成实体类对象
        {
            rt.rdType = Convert.ToInt32(txtrdType.Text);
            rt.rdTypeName = txtrdTypeName.Text;
            rt.CanLendQty = Convert.ToInt32(txtCanLendDay.Text);
            rt.CanLendDay = Convert.ToInt32(txtCanLendQty.Text);
            rt.CanContinueTimes = Convert.ToInt32(txtCanContinueTimes.Text);
            rt.PunishRate = Convert.ToSingle(txtPunishRate.Text);
            rt.DateValid = Convert.ToInt32(txtDateValid.Text);
        }
        #endregion
        
        private void RTQry_Click(object sender, EventArgs e)
        {
            dt = readerTypeBLL.GetAllReaderType();
            ShowData();
        } //查询全部读者类型

        private void RTAdd_Click(object sender, EventArgs e)
        {
            if (txtrdType.Text==""||txtrdTypeName.Text == "" || txtCanLendQty.Text == "" || txtCanLendDay.Text == "" || txtCanContinueTimes.Text == "" || txtPunishRate.Text == "" || txtDateValid.Text == "")
                MessageBox.Show("请将要插入的读者类型信息填写完整！");
            else if (rt.rdTypeName == txtrdTypeName.Text)  
            {
                MessageBox.Show("类型已经存在，请重新输入！");
            }
            else
            {
                SetTextToReaderType(); //将控件上的内容转到实体类中
                readerTypeBLL.AddRT(rt); //添加将readertype添加到数据库中
                dt = readerTypeBLL.GetAllReaderType(); //更新dgeReaderType
                ShowData();
                MessageBox.Show("添加成功！");
            }
        }  //添加读者类型

        private void RTUpdate_Click(object sender, EventArgs e)
        {
            if (txtrdType.Text == "")
                MessageBox.Show("请输入您要更新的读者类别！");
            /*else if (rt.rdType != Convert.ToInt32(txtrdTypeName.Text))
                MessageBox.Show("读者类别不存在，无法更新！");*/
            else
            {
                SetTextToReaderType();
                rt.rdType = Convert.ToInt32(txtrdType.Text); //文本框输入要更新的读者类别号
                readerTypeBLL.UpdateRT(rt);//通过三层体系，更新数据库中读者类别号对应的readertype
                dt = readerTypeBLL.GetAllReaderType();
                ShowData();
                MessageBox.Show("修改成功！");
            }
       }

        private void RTDelete_Click(object sender, EventArgs e)
        {
            if (txtrdType.Text == "")
                MessageBox.Show("请输入您要更新的读者类别！");
           /* else if (rt.rdType != Convert.ToInt32(txtrdTypeName.Text))
                MessageBox.Show("读者类别不存在，无法更新！");*/
            else
            {
                rt.rdType = Convert.ToInt32(txtrdType.Text); 
                readerTypeBLL.Delete(rt);//删除数据库中文本框中读者类别号对应的对象
                MessageBox.Show("成功删除！");
            }
        }

        private void RTBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region 将DataGridView的数据显示到textbox中
        private void dgvReaderType_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvReaderType.CurrentCell == null)
                return;
            rt = ReaderTypeAdmin.GetReaderType(Convert.ToInt32(dgvReaderType["rdType", dgvReaderType.CurrentCell.RowIndex].Value));
            SetReaderTypeToText();
        }
        #endregion
        private void dgvReaderType_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupBox1.Controls)//清除所有textbox的内容
            {
                if (ctrl is TextBox)
                    ctrl.Text = " ";
            }
        }
        private void btnRTQry_Click(object sender, EventArgs e)
        {
            int rdType;
            string rdTypeName;
            if (txtrdType.Text.Trim() == "") //读者类别没写
                rdType = 0;
            else
            {
                int i = txtrdType.Text.IndexOf("--");
                if (i > 0)
                    rdType = Convert.ToInt32(txtrdType.Text.Substring(0, i));
                else
                    rdType = Convert.ToInt32(txtrdType.Text);
            }
            rdTypeName = txtrdTypeName.Text;//类别名为控件内容
            
            dt = readerTypeBLL.GetReaderType(rdType, rdTypeName);//ReaderDAL.GetReader(rdType, rdDept, rdName)的判断语句,无法判断单位和姓名
            ShowData();
        }
    }
}
